package engine

import (
	"github.com/gin-contrib/pprof"
	"github.com/gin-gonic/gin"
	"net/http"
	"sync"
	"time"
)

func StartSingle() {
	r := gin.New()
	r.Use(gin.Logger(), gin.Recovery())
	SetRouter(r)

	pprof.Register(r)

	r.Run(":8080")
}

func StartMultiple() {
	//http.Handler
	r := gin.New()
	r.Use(gin.Logger(), gin.Recovery())
	SetRouter(r)
	pprof.Register(r)

	//http.Handler
	r2 := gin.Default()
	SetRouter(r2)
	pprof.Register(r2)

	server1 := &http.Server{
		Addr:         "8080",
		Handler:      r,
		ReadTimeout:  5 * time.Second,
		WriteTimeout: 5 * time.Second,
	}

	server2 := &http.Server{
		Addr:         "8081",
		Handler:      r2,
		ReadTimeout:  5 * time.Second,
		WriteTimeout: 5 * time.Second,
	}

	wait := sync.WaitGroup{}
	wait.Add(2)
	go func() {
		server1.ListenAndServe()
		wait.Done()
	}()

	go func() {
		server2.ListenAndServe()
		wait.Done()
	}()

	wait.Wait()
}

func StartMediaServer() {
	r := gin.Default()

	/**
	engine.StaticFile(relativePath, filepath string)  //映射文件
	engine.Static(relativePath, root string)  //映射目录下的所有文件，默认不展示目录
	engine.StaticFS(relativePath string, fs http.FileSystem) //映射目录下的所有文件  展示目录
	*/
	r.StaticFS("/media", http.Dir("/Users/fanshuai/media"))
	r.Run(":8080")
}

func StartMediaServer2() {
	/**
	http.FileServer处理静态文件 （目录与文件）
	*/

	fs := http.FileServer(http.Dir("/Users/fanshuai/media"))
	http.Handle("/media/", http.StripPrefix("/media/", fs))
	http.ListenAndServe(":8080", nil)
}
